﻿#region Using Statements
using System;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.Sounds;
#endregion

namespace StarFox.ScreenComponents
{
    /// <summary>
    /// Messages associated to GamePlayScreen (countdown).
    /// </summary>
    public class CountDownMessage : ScreenComponent2D
    {
        #region Fields

        SpriteSheet spriteSheet;
        GamePlaySound sound;

        #endregion

        #region Properties

        TimeSpan countDownDuration = TimeSpan.FromMilliseconds(1000);

        /// <summary>
        /// Sets the duration of the message.
        /// </summary>
        public TimeSpan Duration
        {
            set
            {
                countDownDuration = TimeSpan.FromMilliseconds(value.TotalMilliseconds / 4);
            }
        }

        #endregion

        #region Screen Transitions

        /// <summary>
        /// Makes the ScreenComponent2D transition to visible state.
        /// </summary>
        public override void Show()
        {
        }

        /// <summary>
        /// Makes the ScreenComponent2D transition to hidden state.
        /// </summary>
        public override void Hide()
        {
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that this ScreenComponent2D is associated to.</param>
        public CountDownMessage(Screen screen)
            : base(screen)
        {
            spriteSheet = Game.Content.Load<SpriteSheet>(ContentSettings.GetAsset("MSG_SPRITES"));

            sound = Game.Services.GetService(typeof(GamePlaySound)) as GamePlaySound;
            if (sound == null)
                throw new InvalidOperationException("Service of type StarFoxSound not found");
        }

        #endregion

        #region Countdown

        float countDownTime;
        int countDown = 3;
        float alpha;
        bool isCountDown;

        /// <summary>
        /// Starts a new message.
        /// </summary>
        public void Start()
        {
            isCountDown = true;
        }

        /// <summary>
        /// Raised when the message is finishing.
        /// </summary>
        public event EventHandler<EventArgs> Finishing;

        #endregion

        #region Update

        /// <summary>
        /// Called when the ScreenComponent2D needs to be updated.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Update(GameTime gameTime)
        {
            if (Screen.Paused)
                return;

            if (isCountDown)
            {
                if (countDownTime == 0.0f)
                {
                    if (countDown >= 1)
                        sound.Countdown_Count();
                    else
                        sound.Countdown_Go();
                }

                float timeDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / countDownDuration.TotalMilliseconds);
                countDownTime += timeDelta;
                countDownTime = MathHelper.Clamp(countDownTime, 0.0f, 1.0f);
                alpha = 1.0f - countDownTime;
                if (countDownTime == 1.0f) { countDownTime = 0.0f; countDown--; }
                if (countDown < 0) { isCountDown = false; Finishing(this, EventArgs.Empty); }
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when the ScreenComponent2D needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Draw(GameTime gameTime)
        {
            if (Screen.Paused)
                return;

            Rectangle auxRect;

            if (isCountDown)
            {
                if (countDown > 0)
                {
                    auxRect = spriteSheet.SourceRectangle("COUNTDOWN_READY");
                    SpriteBatch.Draw(spriteSheet.Texture, new Vector2(Screen.Width * 0.5f, Screen.Height * 0.2f), auxRect, Color.Yellow,
                                     0.0f, new Vector2(auxRect.Width / 2, auxRect.Height / 2), Screen.Scale, SpriteEffects.None, 0);

                    switch (countDown)
                    {
                        case 3:
                            auxRect = spriteSheet.SourceRectangle("COUNTDOWN_3");
                            break;
                        case 2:
                            auxRect = spriteSheet.SourceRectangle("COUNTDOWN_2");
                            break;
                        case 1:
                            auxRect = spriteSheet.SourceRectangle("COUNTDOWN_1");
                            break;
                        default:
                            break;
                    }

                    SpriteBatch.Draw(spriteSheet.Texture, new Vector2(Screen.Width * 0.5f, Screen.Height * 0.275f), auxRect, Color.Yellow * alpha,
                                     0.0f, new Vector2(auxRect.Width / 2, auxRect.Height / 2), Screen.Scale, SpriteEffects.None, 0);
                }
                else if (countDown == 0)
                {
                    auxRect = spriteSheet.SourceRectangle("COUNTDOWN_GO");
                    SpriteBatch.Draw(spriteSheet.Texture, new Vector2(Screen.Width * 0.5f, Screen.Height * 0.2f), auxRect, Color.Yellow * alpha,
                                     0.0f, new Vector2(auxRect.Width / 2, auxRect.Height / 2), Screen.Scale, SpriteEffects.None, 0);
                }
            }
        }

        #endregion
    }
}
